package com.hksj.wmp.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.cbm.service.DeviceInstanceService;
import com.hksj.cbm.vo.DeriveMetadataVO;
import com.hksj.wmp.mapper.WeChatMyInfoMapper;
import com.hksj.wmp.query.MyInfoQuery;
import com.hksj.wmp.vo.AlarmNotifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see WeChatMyInfoService
 * @since 2024/10/17 11:49
 */

@Service
public class WeChatMyInfoService {

    @Autowired
    private WeChatMyInfoMapper wechatMyInfoMapper;

    @Autowired
    private DeviceInstanceService deviceInstanceService;

    /**
     * 查询当前用户的报警通知情况
     * @param query 查询条件
     * @return 报警通知集合
     */
    public IPage<AlarmNotifyVO> queryAlarmNotifyHistoryListByPaging(MyInfoQuery query){

        //创建分页对象
        IPage<AlarmNotifyVO> page = new Page<>(query.getPageNum(),query.getPageSize());

        //分页查询当前用户的报警通知列表
        List<AlarmNotifyVO> alarmNotifyList = wechatMyInfoMapper.queryAlarmNotifyHistoryListByPaging(page,query);

        //获取设备id集合
        List<String> deviceIdList = alarmNotifyList.stream().map(AlarmNotifyVO::getDeviceId).collect(Collectors.toList());

        //判断设备是否为空
        if (ObjectUtils.isEmpty(deviceIdList)){
            return page;
        }

        //通过设备id集合 查询设备的物模型
        Map<String, Map<String, DeriveMetadataVO>> deviceAttributeMap = deviceInstanceService.batchQueryDeviceAttribute(deviceIdList);

        //将设备的物模型设置到设备
        alarmNotifyList.forEach(alarmNotify -> {

            //获取设备的物模型属性Map集合
            Map<String, DeriveMetadataVO> devicePropertyMap = deviceAttributeMap.get(alarmNotify.getDeviceId());

            //解析property
            JSONObject propertyMap = JSONObject.parseObject(alarmNotify.getProperty());

            //获取property的所有的key组成的数组
            Set<String> propertySet = propertyMap.keySet();

            if (!ObjectUtils.isEmpty(propertySet)){
                //拿到数组的第一个key 是此时报警的物模型属性
                String property = String.valueOf(propertySet.toArray()[0]);

                //获取该物模型属性对应的值则是当前的报警值
                String alarmValue = String.valueOf(propertyMap.get(property));

                //设置物模型属性
                alarmNotify.setProperty(property);

                //设置当前设备报警值
                alarmNotify.setAlarmValue(alarmValue);

                //在设备物模型属性Map集合中查找物模型信息
                //判断设备物模型属性Map是否为空 为空则跳过设置物模型的单位
                if (ObjectUtils.isEmpty(devicePropertyMap)){
                    return;
                }

                DeriveMetadataVO deriveMetadataVO = devicePropertyMap.get(property);

                //设置物模型名称、单位
                alarmNotify.setPropertyName(deriveMetadataVO.getKeyName());
                alarmNotify.setUnit(deriveMetadataVO.getUnit());

            }

        });



        //将分页结果设置到分页对象中
        page.setRecords(alarmNotifyList);

        //返回分页对象
        return page;
    }
}
